<!DOCTYPE html>
<html>
<head>
<META charset = "utf-8">
<title>角色管理</title>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
<link rel="stylesheet" type="text/css" href="/static/new_ui/new_css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="/static/new_ui/new_css/base_v3.0.css">
<link rel="stylesheet" type="text/css" href="/static/new_ui/new_css/themes.css">
<link rel="stylesheet" type="text/css" href="/static/new_ui/new_css/colpick.css">
<link href="http://ui.fcstatic.naryou.cn/static/libs/easyui/themes/icon.css" rel="stylesheet" type="text/css" />
<link  style="width:16px; height=16px;" rel="shortcut icon" href="/static/new_ui/images/logo_s.png">
<script type="text/javascript" src="/static/new_ui/new_js/jquery.js"></script>
<script type="text/javascript" src="/static/libs/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/static/new_ui/new_js/base_v3.0.js"></script>
<script type="text/javascript" src="/static/new_ui/new_js/bootstrap.min.js"></script>
<script type="text/javascript" src="/static/new_ui/new_js/build.js"></script>
<script type="text/javascript" src="/static/new_ui/new_js/css3-mediaqueries.js"></script>
<script type="text/javascript" src="/static/new_ui/new_js/themes.plugin.js"></script>

<link rel="stylesheet" type="text/css" href="/static/new_ui/new_css/datepicker.css">
<script type="text/javascript" src="/static/new_ui/new_js/datepicker.js"></script>
<script type="text/javascript" src="/static/new_ui/new_js/pagelation.js"></script>
<script type="text/javascript" src="/static/new_ui/new_js/zepto.touchwipe.min.js"></script>
<script type="text/javascript" src="/static/new_ui/new_js/colpick.js"></script>
<script type="text/javascript">
$.extend({  //jquery extend
	turnOnAjax:function(flag,str) {
	   
		var html = "<div class='modal-backdrop fade in' id='ajaxloadimg' style='z-index:20000'><center><img src='/static/new_ui/img/ajaxloadimg.gif' style='margin: auto;width: 50px;margin-top: 20%;'><p style='color: white;font-weight: bold;'>"+str+"</p></center></div>"
		if(flag == 'on'){
			$("body").append(html)
		}else{
			$("#ajaxloadimg").remove();
		}
		},
});

</script>




</head>
<body>
<div class="container-fluid warp tops">
    <h1 class="page-headers no-icon">
        角色管理&nbsp;&nbsp;&nbsp;
            <span class="current">
                <span style="width:339px"><i style="margin-left:-82px"></i>开发商的角色 可选择使用"客立方"权限范围是全部的。<br>销售的角色 可选择使用"客立方"权限范围时是有限的。<br></span>
            </span>
    </h1>
    <div class="row-fluid">
        <ul id="myTab" class="nav nav-tabs">
            <li class="active"><a href="#daiqianyue" data-toggle="tab" onclick="loadData('dev')">开发商</a></li>
            <li><a href="#yanqiqianyue" data-toggle="tab" onclick="loadData('sal')">销售</a></li>
        </ul>
        <div id="myCustom" class="tab-content single-content no-top-border no-th-border">
            <div class="tab-pane fade in active" id="daiqianyue">
                <div class="right-btn">
                    <a class="button btn-success btn-w140 btn-h30" href="#firstPopup" data-toggle="modal"
                        onclick="initSelectData('1',false,null,null,null,'devfristlevel','devfristsencod','devfristthrid');">创建开发商角色</a>
                </div>
                <form></form>
                <table class="table">
                    <tr>
                        <th>角色</th>
                        <th>角色描述</th>
                        <th>权限范围</th>
                        <th>操作</th>
                    </tr>
                </table>
            </div>
            <div class="tab-pane fade" id="yanqiqianyue">
                <div class="right-btn">
                    <a class="button btn-success btn-w140 btn-h30" href="#secondPopup" data-toggle="modal"
                        onclick="initSelectData('2',false,null,null,null,'salefristlevel','salefristsencod','salefristthrid');">创建销售角色</a>
                </div>
                <form></form>
                <table class="table">
                    <tr>
                        <th>角色</th>
                        <th>角色描述</th>
                        <th>权限范围</th>
                        <th>操作</th>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <!--弹出层-->
    <!--创建开发商角色-->
    <div id="firstPopup" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
            <h3 class="PopupTitle">创建开发商角色</h3>
        </div>
        <div class="modal-body">
            <div class="input-cont2" id="devnamewrongDiv">
                <p>
                    <span class="clip-popup-font w65 color-0">角色名称：</span><input type="text" class="in-text2 h40 w220" id="devname" onkeypress="validateRoleName(this,'devname');"
                        onkeyup="validateRoleName(this,'devname');"><span class="txt-num"><i id="devnamewronglength">0</i>/<i>5</i></span> <span class="value-wrong" id="devnamewrong"></span>

                </p>
            </div>
            <div class="input-cont2" id="devdescriptionwrongDiv">
                <p>
                    <span class="clip-popup-font w65 color-0">角色描述：</span><input type="text" class="in-text2 h40 w220" id="devdescription" onkeypress="validateDisName(this,'devdescription');"
                        onkeyup="validateDisName(this,'devdescription');"> <span class="txt-num"><i id="devdescriptionwronglength">0</i>/<i>10</i></span> <span class="value-wrong"
                        id="devdescriptionwrong"></span>
                </p>
            </div>
            <div class="nav-hint" style="margin-bottom: 0;">
                <span class="head-info">角色权限：</span>
                <p class="clearFix">
                    <span class="pull-left">一级菜单</span> <span class="pull-left">二级菜单</span> <span class="pull-left">三级菜单</span>
                </p>
            </div>
            <div class="nav-con">
                <ul class="clearFix">
                    <li class="pull-left left-con" id="devfristlevel"></li>
                    <li class="pull-left center-con none" id="devfristsencod"></li>
                    <li class="pull-left right-con none" id="devfristthrid"></li>
                </ul>
            </div>
        </div>
        <div class="modal-footer">
            <button class="button btn-success btn-w100" type="button" onclick="addRole(this,1,'dev','devname','devdescription')">确定</button>
            <button class="button btn-gray btn-w100" data-dismiss="modal" aria-hidden="true">取消</button>
        </div>
    </div>
    <!--创建销售角色-->
    <div id="secondPopup" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
            <h3 class="PopupTitle">创建销售角色</h3>
        </div>
        <div class="modal-body">
            <div class="input-cont2" id="salenamewrongDiv">
                <p>
                    <span class="clip-popup-font w65 color-0">角色名称：</span><input type="text" class="in-text2 h40 w220" id="salename" onkeypress="validateRoleName(this,'salename');"
                        onkeyup="validateRoleName(this,'salename');"><span class="txt-num"><i id="salenamewronglength">0</i>/<i>5</i></span> <span class="value-wrong" id="salenamewrong"></span>
                </p>
            </div>
            <div class="input-cont2" id="saledescriptionwrongDiv">
                <p>
                    <span class="clip-popup-font w65 color-0">角色描述：</span><input type="text" class="in-text2 h40 w220" id="saledescription" onkeypress="validateDisName(this,'saledescription');"
                        onkeyup="validateDisName(this,'saledescription');"> <span class="txt-num"><i id="saledescriptionwronglength">0</i>/<i>10</i></span> <span class="value-wrong"
                        id="saledescriptionwrong"></span>
                </p>
            </div>
            <div class="nav-hint" style="margin-bottom: 0;">
                <span class="head-info">角色权限：</span>
                <p class="clearFix">
                    <span class="pull-left">一级菜单</span> <span class="pull-left">二级菜单</span> <span class="pull-left">三级菜单</span>
                </p>
            </div>
            <div class="nav-con">
                <ul class="clearFix">
                    <li class="pull-left left-con" id="salefristlevel"></li>
                    <li class="pull-left center-con" id="salefristsencod"></li>
                    <li class="pull-left center-con" id="salefristthrid"></li>
  
                </ul>
            </div>
        </div>
        <div class="modal-footer">
            <button class="button btn-success btn-w100" onclick="addRole(this,2,'sal','salename','saledescription')">确定</button>
            <button class="button btn-gray btn-w100" data-dismiss="modal" aria-hidden="true">取消</button>
        </div>
    </div>
    <!--删除提示-->
    <div id="thirdPopup" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header" style="border: none;">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
            <h3 class="PopupTitle" style="text-align: center;">提示</h3>
            <input type="hidden" id="type" name="type"> <input type="hidden" id="rowId" name="rowId"> <input type="hidden" id="sysRole" name="sysRole"> <input type="hidden"
                id="roleName" name="roleName">
        </div>
        <div class="modal-body">
            <div class="del-hint">
                <img src="/new/images/delete-ico.png" alt="" />
                <p id="deletetitle"></p>
            </div>
        </div>
        <div class="modal-footer" style="width: 224px; margin: 0 auto;">
            <button class="button btn-success btn-w100" type="button" onclick="deleteRole(this)">确定</button>
            <button class="button btn-gray btn-w100" data-dismiss="modal" aria-hidden="true">取消</button>
        </div>
    </div>
    <!--编辑开发商角色-->
    <div id="fourthPopup" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
            <h3 class="PopupTitle">修改开发商角色</h3>
        </div>
        <div class="modal-body">
            <div class="input-cont2" id="editdevnamewrongDiv">
                <p>
                    <span class="clip-popup-font w65 color-0">角色名称：</span><input type="text" class="in-text2 h40 w220" id="editdevname" onkeypress="validateRoleName(this,'editdevname');"
                        onkeyup="validateRoleName(this,'editdevname');"><span class="txt-num"><i id="editdevnamewronglength">0</i>/<i>5</i></span> <span class="value-wrong"
                        id="editdevnamewrong"></span>
                </p>
            </div>
            <div class="input-cont2" id="editdevdescriptionwrongDiv">
                <p>
                    <span class="clip-popup-font w65 color-0">角色描述：</span><input type="text" class="in-text2 h40 w220" id="editdevdescription" onkeypress="validateDisName(this,'editdevdescription');"
                        onkeyup="validateDisName(this,'editdevdescription');"> <span class="txt-num"><i id="editdevdescriptionwronglength">0</i>/<i>10</i></span> <span class="value-wrong"
                        id="editdevdescriptionwrong"></span>
                </p>
            </div>
            <div class="nav-hint" style="margin-bottom: 0;">
                <span class="head-info">角色权限：</span>
                <p class="clearFix">
                    <span class="pull-left">一级菜单</span> <span class="pull-left">二级菜单</span> <span class="pull-left">三级菜单</span>
                </p>
            </div>
            <div class="nav-con">
                <ul class="clearFix">
                    <li class="pull-left left-con" id="editdevfristlevel"></li>
                    <li class="pull-left center-con none" id="editdevfristsencod"></li>
                    <li class="pull-left right-con none" id="editdevfristthrid"></li>
                </ul>
            </div>
        </div>
        <div class="modal-footer">
            <button class="button btn-success btn-w100" onclick="updateRole(this,'dev','editdevname','editdevdescription')">确定</button>
            <button class="button btn-gray btn-w100" data-dismiss="modal" aria-hidden="true">取消</button>
        </div>
    </div>
    <!--编辑销售角色-->
    <div id="fifthPopup" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
            <h3 class="PopupTitle">修改销售角色</h3>
        </div>
        <div class="modal-body">
            <div class="input-cont2" id="editsalenamewrongDiv">
                <p>
                    <span class="clip-popup-font w65 color-0">角色名称：</span><input type="text" class="in-text2 h40 w220" id="editsalename" onkeypress="validateRoleName(this,'editsalename');"
                        onkeyup="validateRoleName(this,'editsalename');"><span class="txt-num"><i id="editsalenamewronglength">0</i>/<i>5</i></span> <span class="value-wrong"
                        id="editsalenamewrong"></span>
                </p>
            </div>
            <div class="input-cont2" id="editsaledescriptionwrongDiv">
                <p>
                    <span class="clip-popup-font w65 color-0">角色描述：</span> <input type="text" class="in-text2 h40 w220" id="editsaledescription"
                        onkeypress="validateDisName(this,'editsaledescription');" onkeyup="validateDisName(this,'editsaledescription');"> <span class="txt-num"><i
                        id="editsaledescriptionwronglength">0</i>/<i>10</i></span> <span class="value-wrong" id="editsaledescriptionwrong"></span>
                </p>
            </div>
            <div class="nav-hint" style="margin-bottom: 0;">
                <span class="head-info">角色权限：</span>
                <p class="clearFix">
                    <span class="pull-left">一级菜单</span> <span class="pull-left">二级菜单</span> <span class="pull-left">三级菜单</span>
                </p>
            </div>
            <div class="nav-con">
                <ul class="clearFix">
                    <li class="pull-left left-con" id="editsalefristlevel"></li>
                    <li class="pull-left center-con none" id="editsalefristsencod"></li>
                    <li class="pull-left right-con none" id="editsalefristthrid"></li>
                </ul>
            </div>
        </div>
        <div class="modal-footer">
            <button class="button btn-success btn-w100" onclick="updateRole(this,'sal','editsalename','editsaledescription')">确定</button>
            <button class="button btn-gray btn-w100" data-dismiss="modal" aria-hidden="true">取消</button>
        </div>
    </div>

</div>
<script type="text/javascript">
    var seletMap = new Array();
    var seletGroupMap = new Array();
    var currentRole = 0;
    $(function() {
        $(".left-con .current .check-box").live('click', function() {
            var $that = $(this);
            var $nextNode = $that.parent().parent().nextAll().find('p').find('.check-box');
            if (!$that.hasClass('selected')) {
                $nextNode.removeClass('selected');
            } else {
                $nextNode.addClass('selected');
            }
        });

        $(".center-con .current .check-box").live('click', function() {
            var $that = $(this);
            var $nextNode = $that.parent().parent().nextAll().find('p').find('.check-box');
            if (!$that.hasClass('selected')) {
                $nextNode.removeClass('selected');
            } else {
                $nextNode.addClass('selected');
            }
        });

        var pH = 46, minH = 360;
        $(".left-con").each(function() {
            var pLen = $(this).find('p').length;
            var leftH = pH * pLen;
            leftH = leftH > minH ? leftH : minH;
            $(this).css('height', leftH).nextAll().css('height', leftH);
        });
        $(".left-con p .check-box-name").live('click', function() {
            var $that = $(this);
            var $parentsNode = $that.parent().parent();
            var $nextNode = $parentsNode.next();
            var $parentNode = $that.parent();
            var leftH = $parentsNode.find('p').length * pH;
            if ($that.hasClass('current')) {
                $parentNode.removeClass('current');
                $nextNode.addClass('none');
            } else {
                $nextNode.next().addClass('none');
                $parentsNode.find('p').removeClass('current');
                $parentNode.addClass('current');
                $nextNode.removeClass('none');
            }
            var centerH = $nextNode.find('p').length * pH;
            centerH = centerH > leftH ? centerH : leftH;
            $parentsNode.parent().find('li').css('height', centerH);
        });
        $(".center-con p .check-box-name").live('click', function() {
            var $that = $(this);
            var $parentsNode = $that.parent().parent();
            var $nextNode = $parentsNode.next();
            var $prevNode = $parentsNode.prev();
            var $parentNode = $that.parent();
            var leftH = $prevNode.find('p').length * pH;
            var centerH = $parentsNode.find('p').length * pH;
            if ($that.hasClass('current')) {
                $parentNode.removeClass('current');
                $nextNode.addClass('none');
            } else {
                $parentsNode.find('p').removeClass('current');
                $parentNode.addClass('current');
                $nextNode.removeClass('none');
            }
            var rightH = $nextNode.find('p').length * pH;
            rightH = Math.max(leftH, centerH, rightH);
            $parentsNode.parent().find('li').css('height', rightH);
        });
    });
</script>
<script type="text/javascript">
    function developer() {
        $('#daiqianyue')
                .pageLation(
                        {
                            container : "#daiqianyue",//table外面的div对象的ID
                            url : '/data/json/roles/v3.0',//请求地址
                            pageForm : "",//附加条件的form表单
                            param : {
                                groupId : "1",
                            },
                            success : function(msg, o) {
                                var str = "";
                                for (var i = 0; i < msg.length; i++) {
                                    var row = msg[i];
                                    var merchantResources = msg[i].merchantResources;
                                    var resources = "";
                                    if (!row.sysRole) {
                                        for (var j = 0; j < merchantResources.length; j++) {
                                            var mrow = merchantResources[j];
                                            if (mrow.level == "3") {
                                                continue;
                                            }
                                            resources += mrow.name + ',';
                                        }
                                        resources = resources.substr(0, resources.lastIndexOf(","));
                                    } else {
                                        resources = "显示所有功能";
                                    }
                                    var description = (msg[i].description == null ? "" : msg[i].description);
                                    str += '<tr>' + '<td>' + msg[i].name + '</td>' + '<td>' + description + '</td>' + '<td class="long-width">' + resources + '</td><td>';
                                    if (!row.sysRole) {
                                        str += '<a class="pass color-green"  href="#fourthPopup" data-toggle="modal" onclick="initSelectData(\'1\',true,'
                                                + msg[i].id
                                                + ',\''
                                                + msg[i].name
                                                + '\',\''
                                                + description
                                                + '\',\'editdevfristlevel\',\'editdevfristsencod\',\'editdevfristthrid\');">编辑</a><a class="pass color-red" href="#thirdPopup" data-toggle="modal" onclick="doDele(\'dev\','
                                                + msg[i].id + ',\'' + msg[i].name + '\',' + msg[i].sysRole + ')">删除</a>';
                                    }
                                    str += '</td></tr>';
                                }
                                $.appendData(str, o);
                            }
                        });
    }

    function sale() {
        $('#yanqiqianyue')
                .pageLation(
                        {
                            container : "#yanqiqianyue",//table外面的div对象的ID
                            url : '/data/json/roles/v3.0',//请求地址
                            pageForm : "",//附加条件的form表单
                            param : {
                                groupId : "2",
                            },
                            success : function(msg, o) {
                                var str = "";
                                for (var i = 0; i < msg.length; i++) {
                                    var row = msg[i];
                                    var merchantResources = msg[i].merchantResources;
                                    var resources = "";
                                    for (var j = 0; j < merchantResources.length; j++) {
                                        var mrow = merchantResources[j];
                                        if (mrow.level == "3") {
                                            continue;
                                        }
                                        resources += mrow.name + ',';
                                    }
                                    resources = resources.substr(0, resources.lastIndexOf(","));
                                    var description = (msg[i].description == null ? "" : msg[i].description);
                                    str += '<tr>'
                                            + '<td>'
                                            + msg[i].name
                                            + '</td>'
                                            + '<td>'
                                            + description
                                            + '</td>'
                                            + '<td class="long-width">'
                                            + resources
                                            + '</td>'
                                            + '<td><a class="pass color-green" href="#fifthPopup" data-toggle="modal" onclick="initSelectData(\'2\',true,'
                                            + msg[i].id
                                            + ',\''
                                            + msg[i].name
                                            + '\',\''
                                            + description
                                            + '\',\'editsalefristlevel\',\'editsalefristsencod\',\'editsalefristthrid\');">编辑</a><a class="pass color-red" href="#thirdPopup" data-toggle="modal" onclick="doDele(\'sal\','
                                            + msg[i].id + ',\'' + msg[i].name + '\',' + msg[i].sysRole + ')">删除</a></td></tr>';
                                }
                                $.appendData(str, o);
                            }
                        });
    }

    function loadData(type) {
        if (type == "dev") {
            developer();
        } else if (type == "sal") {
            sale();
        } else {
            developer();
        }
    }
    //加载数据
    loadData();

    /**
     * 删除角色
     */
    function deleteRole(obj) {
        var type = $("#type").val();
        var rowId = $("#rowId").val();
        var sysRole = $("#sysRole").val();
        if (sysRole == "true") {
            statusInfo($(obj), 2, "该角色是系统角色不能删除");
            return;
        }
        var url = "/merse/role/" + rowId + "/";
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : {
                '_method' : 'delete'
            },
            dataType : 'json',
            success : function(data) {
                if (data.success) {
                    statusInfo($(obj), 1, "删除成功");
                    loadData(type);
                    $("#type").val("");
                    $("#rowId").val("");
                    $("#rowName").val("");
                    $("#sysRole").val("");
                } else {
                    statusInfo($(obj), 2, data.msg);
                }
            }
        });

    }

    function doDele(type, rowId, rowName, sysRole) {
        $("#deletetitle").html("确认删除：" + rowName + "?");
        $("#type").val(type);
        $("#rowId").val(rowId);
        $("#rowName").val(rowName);
        $("#sysRole").val(sysRole);
    }
    //创建一级菜单
    function createDevRes(roleGroupId, firsDiv, secoLevelDiv, thirdDiv) {
        var url = "/data/json/resources/firstLevel";
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : {
                groupId : roleGroupId
            },
            dataType : 'json',
            success : function(data) {
                var result;
                try {
                    result = eval(data);
                } catch (e) {
                    result = JSON.parse(data);
                }
                var str = "";
                $("#" + firsDiv).empty();
                for (var i = 0; i < result.length; i++) {
                    var row = result[i];
                    if (isGroupSelected(row.id)) {
                        str += '<p><span class="check-box selected" onclick="changData(1,' + row.id + ',' + row.id + ',this,0,' + roleGroupId + ')"><i></i></span>&nbsp;&nbsp;'
                                + '<a class="check-box-name" href="javascript:void(0);" onclick="createDevSecond(0,1,' + row.id + ',' + roleGroupId + ',\'' + secoLevelDiv + '\',\'' + thirdDiv
                                + '\')">' + row.name + '</a></p>';
                    } else {
                        str += '<p><span class="check-box" onclick="changData(1,' + row.id + ',' + row.id + ',this,0,' + roleGroupId + ')"><i></i></span>&nbsp;&nbsp;'
                                + '<a class="check-box-name" href="javascript:void(0);" onclick="createDevSecond(0,1,' + row.id + ',' + roleGroupId + ',\'' + secoLevelDiv + '\',\'' + thirdDiv
                                + '\')">' + row.name + '</a></p>';
                    }

                }
                $("#" + firsDiv).append(str);

            }
        });
    }
    //创建第二级菜单
    function createDevSecond(parentId, level, groupId, roleGroupId, secoLevelDiv, thirdDiv) {
        var url = "/data/json/resources/otherLevel";
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : {
                groupId : groupId,
                parentId : parentId,
                level : level,
                roleGroupId : roleGroupId
            },
            dataType : 'json',
            success : function(data) {
                var result;
                try {
                    result = eval(data);
                } catch (e) {
                    result = JSON.parse(data);
                }
                var str = "";
                $("#" + secoLevelDiv).empty();
                for (var i = 0; i < result.length; i++) {
                    var row = result[i];
                    if (isSelected(row.id)) {
                        str += '<p><span class="check-box selected" onclick="changData(2,' + row.id + ',' + groupId + ',this,0,' + roleGroupId + ')"><i></i></span>&nbsp;&nbsp;'
                                + '<a class="check-box-name" href="javascript:void(0);" onclick="createDevThrid(' + row.id + ',2,' + groupId + ',' + roleGroupId + ',\'' + thirdDiv + '\')">'
                                + row.name + '</a></p>';
                    } else {
                        str += '<p><span class="check-box" onclick="changData(2,' + row.id + ',' + groupId + ',this,0,' + roleGroupId + ')"><i></i></span>&nbsp;&nbsp;'
                                + '<a class="check-box-name" href="javascript:void(0);" onclick="createDevThrid(' + row.id + ',2,' + groupId + ',' + roleGroupId + ',\'' + thirdDiv + '\')">'
                                + row.name + '</a></p>';
                    }

                }
                $("#" + secoLevelDiv).append(str);

            }
        });
    }
    //创建第三级菜单
    function createDevThrid(parentId, level, groupId, roleGroupId, thirdDiv) {
        var url = "/data/json/resources/otherLevel";
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : {
                groupId : groupId,
                parentId : parentId,
                level : level,
                roleGroupId : roleGroupId
            },
            dataType : 'json',
            success : function(data) {
                var result;
                try {
                    result = eval(data);
                } catch (e) {
                    result = JSON.parse(data);
                }
                var str = "";
                $("#" + thirdDiv).empty();
                for (var i = 0; i < result.length; i++) {
                    var row = result[i];
                    if (isSelected(row.id)) {
                        str += '<p><span class="check-box selected" onclick="changData(3,' + row.id + ',0,this,' + parentId + ',' + roleGroupId + ')"><i></i></span>&nbsp;&nbsp;'
                                + '<a class="check-box-name" href="javascript:void(0);" ">' + row.name + '</a></p>';
                    } else {
                        str += '<p><span class="check-box" id="devThrid' + i + '" onclick="changData(3,' + row.id + ',0,this,' + parentId + ',' + roleGroupId + ')"><i></i></span>&nbsp;&nbsp;'
                                + '<a class="check-box-name" href="javascript:void(0);" ">' + row.name + '</a></p>';
                    }
                }
                $("#" + thirdDiv).append(str);

            }
        });
    }
    //初始化数据
    function initSelectData(type, isEdit, roleId, roleName, roleDescription, firsDiv, secoLevelDiv, thirdDiv) {
        $("#" + firsDiv).empty();
        $("#" + secoLevelDiv).empty();
        $("#" + thirdDiv).empty();
        seletGroupMap = new Array();
        seletMap = new Array();
        currentRole = 0;
        if (isEdit) {
            if (type == 1) {
                $("#editdevname").val(roleName);
                $("#editdevnamewrongDiv").removeClass("wrong");
                $("#editdevnamewrong").html('');
                $("#editdevnamewronglength").html(roleName.length);

                $("#editdevdescriptionwrongDiv").removeClass("wrong");
                $("#editdevdescriptionwrong").html('');
                $("#editdevdescriptionwronglength").html(roleDescription.length);
                $("#editdevdescription").val(roleDescription);
            } else if (type == 2) {
                $("#editsalename").val(roleName);
                $("#editsalenamewrongDiv").removeClass("wrong");
                $("#editsalenamewrong").html('');
                $("#editsalenamewronglength").html(roleName.length);

                $("#editsaledescriptionwrongDiv").removeClass("wrong");
                $("#editsaledescriptionwrong").html('');
                $("#editsaledescriptionwronglength").html(roleDescription.length);
                $("#editsaledescription").val(roleDescription);
            }
            currentRole = roleId;
            //通过角色查询下面的权限
            initSelectedResource(roleId);
            createDevRes(type, firsDiv, secoLevelDiv, thirdDiv);
            //查询数据
        } else {

            if (type == 1) {
                $("#devnamewrongDiv").removeClass("wrong");
                $("#devnamewrong").html('');
                $("#devnamewronglength").html('0');
                $("#devdescriptionwrongDiv").removeClass("wrong");
                $("#devdescriptionwrong").html('');
                $("#devdescriptionwronglength").html('0');
                $("#devname").val("");
                $("#devdescription").val("");
            } else if (type == 2) {
                $("#salename").val(roleName);
                $("#salenamewrongDiv").removeClass("wrong");
                $("#salenamewrong").html('');
                $("#salenamewronglength").html('0');
                $("#saledescriptionwrongDiv").removeClass("wrong");
                $("#saledescriptionwrong").html('');
                $("#saledescriptionwronglength").html('0');
                $("#saledescription").val(roleDescription);
            }
            createDevRes(type, firsDiv, secoLevelDiv, thirdDiv);

        }
    }
    //改变数据 
    function changData(level, id, groupId, obj, parentId, roleGroupId) {
        var className = $(obj).attr("class");
        var url = "/data/json/resources/otherLevel";
        if (level == 1) {
            $.ajax({
                url : url,
                async : false,
                type : 'post',
                data : {
                    groupId : id,
                    parentId : 0,
                    level : "1,2",
                    roleGroupId : roleGroupId
                },
                dataType : 'json',
                success : function(data) {
                    var result;
                    try {
                        result = eval(data);
                    } catch (e) {
                        result = JSON.parse(data);
                    }
                    if (className == "check-box") {
                        for (var i = 0; i < result.length; i++) {
                            //选中                
                            seletMap.push(result[i].id);
                        }
                    } else {
                        //取消   
                        for (var i = 0; i < result.length; i++) {
                            cancelSelected(result[i].id);
                        }

                    }

                }
            });

        } else if (level == 2) {
            $.ajax({
                url : url,
                async : false,
                type : 'post',
                data : {
                    groupId : groupId,
                    parentId : id,
                    level : "2",
                    roleGroupId : roleGroupId
                },
                dataType : 'json',
                success : function(data) {
                    var result;
                    try {
                        result = eval(data);
                    } catch (e) {
                        result = JSON.parse(data);
                    }

                    if (className == "check-box") {
                        seletMap.push(id);
                        for (var i = 0; i < result.length; i++) {
                            //选中                
                            seletMap.push(result[i].id);
                        }
                    } else {
                        cancelSelected(id);
                        //取消   
                        for (var i = 0; i < result.length; i++) {
                            cancelSelected(result[i].id);
                        }

                    }

                }
            });

        } else if (level == 3) {

            if (className == "check-box") {
                //选中                
                seletMap.push(id);
            } else {
                //取消    			
                cancelSelected(id);
            }
            //添加父级ID
            addParent(parentId);
        }

    }
    //添加父级ID
    function addParent(parentId) {
        var flag = false;
        for (var i = 0; i < seletMap.length; i++) {
            if (parentId == seletMap[i]) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            seletMap.push(parentId);
        }
    }
    //判断是否选中
    function isSelected(id) {
        for (var i = 0; i < seletMap.length; i++) {
            if (id == seletMap[i]) {
                return true;
            }
        }
        return false;
    }
    //判断是否选中
    function isGroupSelected(id) {
        for (var i = 0; i < seletGroupMap.length; i++) {
            if (id == seletGroupMap[i]) {
                return true;
            }
        }
        return false;
    }

    //取消选择
    function cancelSelected(id) {
        newMap = new Array();
        for (var i = 0; i < seletMap.length; i++) {
            if (id == seletMap[i]) {
                continue;
            }
            newMap.push(seletMap[i]);
        }
        seletMap = newMap;
    }

    //添加开发角色
    function addRole(obj, groupId, type, name, description) {
        if ($.trim($("#" + name).val()) == "") {
            $("#" + name + "wrongDiv").addClass("wrong");
            $("#" + name + "wrong").html('角色名称不能为空');
            return;
        } else {
            $("#" + name + "wrongDiv").removeClass("wrong");
            $("#" + name + "wrong").html('');
        }
        if (!validateRoleName($("#" + name), name)) {
            return;
        }

        if (!validateDisName($("#" + description), description)) {
            return;
        }
        if (seletMap.length == 0) {
            saveResult(false, "角色权限不能为空");
            return;
        }
        $.ajax({
            url : "/merse/role/V3.0/create.htm/",
            async : false,
            type : 'post',
            data : {
                name : $("#" + name).val(),
                description : $("#" + description).val(),
                resources : seletMap.join(","),
                groupId : groupId
            },
            dataType : 'json',
            success : function(data) {
                var result;
                try {
                    result = eval(data);
                } catch (e) {
                    result = JSON.parse(data);
                }
                if (data.success) {
                    statusInfo($(obj), 1, "添加成功");
                    loadData(type);
                } else {
                    saveResult(false, data.msg);
                }
            }
        });
    }

    //编辑角色
    function updateRole(obj, type, name, description) {
        if ($.trim($("#" + name).val()) == "") {
            $("#" + name + "wrongDiv").addClass("wrong");
            $("#" + name + "wrong").html('角色名称不能为空');
            return;
        } else {
            $("#" + name + "wrongDiv").removeClass("wrong");
            $("#" + name + "wrong").html('');
        }

        if (!validateRoleName($("#" + name), name)) {
            return;
        }

        if (!validateDisName($("#" + description), description)) {
            return;
        }

        if (seletMap.length == 0) {
            saveResult(false, "角色权限不能为空");
            return;
        }
        $.ajax({
            url : "/merse/role/V3.0/" + currentRole + "/",
            async : false,
            type : 'post',
            data : {
                name : $("#" + name).val(),
                description : $("#" + description).val(),
                resources : seletMap.join(",")

            },
            dataType : 'json',
            success : function(data) {
                var result;
                try {
                    result = eval(data);
                } catch (e) {
                    result = JSON.parse(data);
                }
                if (data.success) {
                    statusInfo($(obj), 1, "更新成功");
                    loadData(type);
                } else {
                    saveResult(false, data.msg);
                    //statusInfo($(obj), 2, data.msg);
                }
            }
        });
    }
    //通过角色查询下面的权限
    function initSelectedResource(roleId) {
        var url = "/data/json/role/resources/";
        $.ajax({
            url : url,
            async : false,
            type : 'post',
            data : {
                roleId : roleId
            },
            dataType : 'json',
            success : function(data) {
                var result;
                try {
                    result = eval(data);
                } catch (e) {
                    result = JSON.parse(data);
                }
                var str = "";
                for (var i = 0; i < result.length; i++) {
                    seletMap.push(result[i].id);
                    seletGroupMap.push(result[i].resourceGroup.id);
                }
            }
        });
    }

    function validateRoleName(obj, id) {
        obj = $(obj);
        var len = obj.val().length;
        $('#' + id + 'wronglength').html(len);
        if (obj.val() == '超级管理员') {
            $("#" + id + "wrongDiv").addClass("wrong");
            $("#" + id + "wrong").html('角色名称不能为超级管理员');
            return false;
        }
        if (len > 0 && len <= 5) {
            $("#" + id + "wrongDiv").removeClass("wrong");
            $("#" + id + "wrong").html('');
            return true;
        }
        if (len > 5) {
            $("#" + id + "wrongDiv").addClass("wrong");
            $("#" + id + "wrong").html('角色名称不能超过5个字符');
            return false;
        }
    }

    function validateDisName(obj, id) {
        obj = $(obj);
        var len = obj.val().length;
        $('#' + id + 'wronglength').html(len);
        if (len >= 0 && len <= 10) {
            $("#" + id + "wrongDiv").removeClass("wrong");
            $("#" + id + "wrong").html('');
            return true;
        }
        if (len > 10) {
            $("#" + id + "wrongDiv").addClass("wrong");
            $("#" + id + "wrong").html('角色描述不能超过10个字符');
            return false;
        }
    }
</script>
</body>
</html>